// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.Payable;
import com.fowo.api.entity.Receivable;
import com.fowo.api.entity.StockIn;
import com.fowo.api.entity.StockOut;
import com.fowo.api.mapper.ReceivableMapper;
import com.fowo.api.mapper.StockInMapper;
import com.fowo.api.mapper.StockOutMapper;
import com.fowo.api.model.payable.PayableVo;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 应付单 服务实现类
 */
@Service
public class PayableServiceImpl extends PayableBaseServiceImpl {
    @Resource
    private StockInBaseServiceImpl stockInBaseServiceImpl;
    @Resource
    private com.fowo.api.mapper.StockInMapper stockInMapper;
    @Resource
    private  StockOutBaseServiceImpl stockOutBaseServiceImpl;
    @Resource
    private StockOutMapper stockOutMapper;
    @Resource
    private  ReceivableBaseServiceImpl receivableBaseServiceImpl;
    @Resource
    private ReceivableMapper receivableMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(PayableVo model) throws Exception {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        Payable entity = this.getById(model.getId());
        Assert.isTrue(entity.getStauts().equals("0"),"已审核不能修改!");
        if (entity == null) {
            throw new Exception("应付单不存在");
        }
        BeanUtils.copyProperties(
                model,
                entity,
                "id",
                "createTime",
                "createUser",
                "lastUpdateTime",
                "lastUpdateUser"
        );
        entity.setLastUpdateTime(new Date());
        if (currentUser != null) {
            entity.setLastUpdateUser(currentUser.getUserId());
        }
        if (this.updateById(entity)) {
            updateAllChilds(model);
            baseMapper.updateCalcFields(entity.getId());
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(Long id) throws Exception {
        Payable entity = this.getById(id);
        Assert.isTrue(entity.getStauts().equals("0"),"已审核不能删除!");
        return this.removeById(id);
    }



    public void xiuigaishuju(){
        //应付单
        List<Payable> list = this.list();
        for(int i=0;i<list.size();i++){
            Payable payable = list.get(i);
            baseMapper.updateCalcFields(payable.getId());
        }
        //入库单
        List<StockIn> list1 = stockInBaseServiceImpl.list();
        for(int i=0;i<list1.size();i++){
            StockIn stockIn = list1.get(i);
            stockInMapper.updateCalcFields(stockIn.getId());
        }
        //出库单
        List<StockOut> list2 = stockOutBaseServiceImpl.list();
        for(int i=0;i<list2.size();i++){
            StockOut stockOut = list2.get(i);
            stockOutMapper.updateCalcFields(stockOut.getId());
        }
        List<Receivable> list3 = receivableBaseServiceImpl.list();
        for(int i=0;i<list3.size();i++){
            Receivable receivable = list3.get(i);
            receivableMapper.updateCalcFields(receivable.getId());
        }

    }

}
